package com.syl.practice.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectTest {
    public static void main(String[] args) {
        demo01();
    }

    public static void demo01() {
        try {
            Class<?> petCla = Class.forName("com.syl.practice.reflect.Pet");
            Pet pet = (Pet) petCla.newInstance();
            Method m1 = petCla.getMethod("m1", null);
            String s1 = m1.invoke(pet).toString();
            System.out.println(s1);

            Method m2 = petCla.getMethod("m2", String.class);
            String s2 = m2.invoke(pet, "hhe").toString();
            System.out.println(s2);

            Method m3 = petCla.getMethod("m3", String.class, Integer.class);
            String s3 = m3.invoke(pet, "haha", 1).toString();
            System.out.println(s3);

            Field f = petCla.getDeclaredField("colour");
            f.setAccessible(true);
            f.set(pet,"黑色");
            System.out.println((String)f.get(pet));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
